<?php
namespace RMD\AccountBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

use RMD\AccountBundle\Form\Type\SubscriberType;
use RMD\AccountBundle\Entity\Subscriber;

class SubscribersController extends Controller
{
	public function indexAction($listid){
		$list = $this->getDoctrine()->getRepository('RMDAccountBundle:Lists')->find($listid);
		if (!$list || !$list->isOwnedBy($this->getUser())) {
			throw $this->createNotFoundException();
		}
		$subscriber = new Subscriber();
		$subscriber->setList($list);
		$form = $this->createForm(new SubscriberType(), $subscriber);
		return $this->render('RMDAccountBundle:Subscribers:index.html.php',
				array('form' => $form->createView(),
					'subscribers' => $list->getSubscribers(),
					'groupSets' => $list->getGroupSets(),
					'listid' => $listid,
					'listTitle' => $list->getTitle(),));
	}
	public function addAction($listid){
		$list = $this->getDoctrine()->getRepository('RMDAccountBundle:Lists')->find($listid);
		if (!$list || !$list->isOwnedBy($this->getUser())) {
			throw $this->createNotFoundException();
		}
		$subscriber = new Subscriber();
		$subscriber->setList($list);
		$form = $this->createForm(new SubscriberType(), $subscriber);
		$request = $this->getRequest();
		if ($request->isMethod('POST')) {
			$form->bind($request);
			if ($form->isValid()) {
				$session = $this->get('session');
				$em = $this->getDoctrine()->getManager();				
				$em->persist($subscriber);
				$em->flush();
				$session->setFlash('success', 'Subscriber added');				
				return $this->redirect($this->generateUrl('account_subscribers', array('listid' => $listid)));
			}
		}
		return $this->render('RMDAccountBundle:Subscribers:add.html.php',
		array('form' => $form->createView(), 'listid' => $listid));
	}
	public function deleteAction($id){
		$subscriber = $this->getDoctrine()->getRepository('RMDAccountBundle:Subscriber')->find($id);
		$session = $this->get('session');
		if (!$subscriber || !$subscriber->getList()->isOwnedBy($this->getUser())) {
			$session->setFlash('error', 'Given subscriber not found');				
			return $this->redirect($this->generateUrl('account_lists'));
		}
		$listId = $subscriber->getList()->getId();
		$em = $this->getDoctrine()->getManager();
		$em->remove($subscriber);
		/*$query = $em->createQuery(
			'DELETE FROM RMDAccountBundle:Subscriber s WHERE s.id = :id'
		)->setParameter('id', $id);

		$query->getResult();*/
		$em->flush();
		$session->setFlash('success', 'Subscriber deleted');
		return $this->redirect($this->generateUrl('account_subscribers', array('listid' => $listId)));
	}
	public function editAction($id){
		$subscriber = $this->getDoctrine()->getRepository('RMDAccountBundle:Subscriber')->find($id);
		if (!$subscriber || !$subscriber->getList()->isOwnedBy($this->getUser())) {
			throw $this->createNotFoundException();
		}
		$listId = $subscriber->getList()->getId();
		$form = $this->createForm(new SubscriberType(), $subscriber);
		$request = $this->getRequest();
		if ($request->isMethod('POST')) {
			$types = $form->getTypes();
			$originalGroups = array();
			foreach ($subscriber->getGroups() as $group) {
				$originalGroups[] = $group;
			}		
			$form->bind($request);
			if ($form->isValid()) {				
				//add groups
				foreach ($types[1]->getUnmappedFields() as $fieldName) {
					$data = $form->get($fieldName)->getData();
					if (is_array($data)) {				
						foreach ($data as $groupId) {
							$group = $this->getDoctrine()->getRepository('RMDAccountBundle:Group')->find($groupId);
							if (!$group || !$group->getGroupSet()->getList()->isOwnedBy($this->getUser())) {
								throw $this->createNotFoundException();
							}
							foreach ($originalGroups as $key => $origGroup) {	
								if ($origGroup->getId() === $groupId) {
									unset($originalGroups[$key]);
								}
							}
							$subscriber->addGroup($group);
						}
					} else {
						$group = $this->getDoctrine()->getRepository('RMDAccountBundle:Group')->find($data);
						if (!$group || !$group->getGroupSet()->getList()->isOwnedBy($this->getUser())) {
							throw $this->createNotFoundException();
						}
						foreach ($originalGroups as $key => $origGroup) {	
							if ($origGroup->getId() === $data) {
								unset($originalGroups[$key]);
							}
						}
						$subscriber->addGroup($group);
					}
				}
				//remove groups
				foreach ($originalGroups as $origGroup) {
					$subscriber->removeGroup($origGroup);
				}
				$em = $this->getDoctrine()->getManager();
				$em->persist($subscriber);
				$em->flush();
				$session = $this->get('session');
				$session->setFlash('success', 'Subscriber modified');
				return $this->redirect($this->generateUrl('account_subscribers', array('listid' => $listId)));
			}
		}
		return $this->render('RMDAccountBundle:Subscribers:edit.html.php',
				array('form' => $form->createView(), 'id' => $id, 'listid' => $listId));
	}
}
